/**
 *  @file SDLHighScoreSection.cpp
 *
 *  @date 21-nov-2009
 *  @author Ives van der Flaas
 */

#include <iostream>
#include <SDL.h>
#include <tr1/memory>
#include "SDLHighScoreSection.h"
#include "SDLRectangleRepresentation.h"
#include "SDLWindow.h"
#include "SDLTimer.h"
#include "SDLFader.h"
#include "SDLColor.h"
#include "SDLZoomDrawer.h"
#include "SDLRectangleRepresentation.h"
#include "SDLColor.h"
#include "SDLHighScoreView.h"
#include "highScoreController.h"
#include "highScoreView.h"
#include "highScoreModel.h"


namespace si_sdl
{

SDLHighScoreSection::SDLHighScoreSection(
		SDLApplication& parent,
		SDLWindow& win):
			SDLApplicationSection(parent, win)


{
	fController = std::tr1::shared_ptr<si::HighScoreController>(
							new si::HighScoreController(
									std::tr1::shared_ptr<si::HighScoreModel>(new si::HighScoreModel()),
									std::tr1::shared_ptr<si::HighScoreView>(new SDLHighScoreView())));

	fHeader = std::tr1::shared_ptr<SDLTextRepresentation>(
							new SDLTextRepresentation(
									"Highscores",
									"fonts/Vera.ttf",
									40,
									Color(255,255,255),
									Color(0,0,0),
									ALIGN_LEFT));

	fEnterName = std::tr1::shared_ptr<SDLTextRepresentation>(
								new SDLTextRepresentation(
										"Congratulations, you made the highscores!\n"
										"Please enter your name:",
										"fonts/Vera.ttf",
										20,
										Color(255,255,255),
										Color(0,0,0),
										ALIGN_LEFT));

	fInputField = std::tr1::shared_ptr<SDLInputField>(
							new SDLInputField(si::Vector2(80,150)));

}

SDLHighScoreSection::~SDLHighScoreSection()
{
	// TODO Auto-generated destructor stub
}

void SDLHighScoreSection::newFrame(double timeSinceLastFrame)
{
	SDLRectangleRepresentation::drawRect(
			si::Vector2(0,0),
			SDLZoomDrawer::get()->requestDrawableRegion(),
			Color(0,0,0));

	fHeader->draw(si::Vector2(80,20), si::Vector2(0,0));

	switch(fMode)
	{
	case ENTERING:
		fInputField->draw();
		fEnterName->draw(si::Vector2(80,80), si::Vector2(0,0));
		break;
	case VIEWING:
		fController->draw();
		break;
	}


}

void SDLHighScoreSection::youreStarting()
{
	fInputField->reset();

	if(fParent.didTheGameEnd())
	{
		// The game ended. See if we got highscore.
		int score = fParent.getLastScore();

		if(fController->canBeInHighScores(score))
			fMode = ENTERING;
		else
			fMode = VIEWING;
	}
	else
		fMode = VIEWING;
}


void SDLHighScoreSection::onSDLEvent(SDL_Event & event)
{
	SDLApplicationSection::onSDLEvent(event); // make sure SDL_QUIT's are caught...

	// You can enter spaces in your name...
	if(fMode == VIEWING)
	{
		// When user presses space goto menu
		if(event.type == SDL_KEYDOWN)
			if(event.key.keysym.sym == SDLK_SPACE || event.key.keysym.sym == SDLK_ESCAPE)
				fParent.changeSectionTo(si::MENU);
	}

	// Only send keypresses when we're entering new highscores
	if(fMode == ENTERING)
	{
		fInputField->onSDLEvent(event);


		// If we're in enterin mode and user presses ENTER, go to viewing mode!
		if(event.type == SDL_KEYDOWN)
			if(event.key.keysym.sym == SDLK_RETURN)
			{
				fController->addHighScore(fInputField->getContents(), fParent.getLastScore());
				fMode = VIEWING;
			}
	}
}

}
